package x.y.z;

public class LeetCode_1094 {
    public boolean carPooling(int[][] trips, int capacity) {
        final int max = 1000;
        int[] diff = new int[max + 1];
        for (int[] trip : trips) {
            diff[trip[1]] += trip[0];
            diff[trip[2]] -= trip[0];
        }
        int prefixSum = 0;
        for (int i = 0; i <= max; i++) {
            prefixSum += diff[i];
            if (prefixSum > capacity) {
                return false;
            }
        }
        return true;
    }
}
